package com.example.demo.config;

import com.example.demo.gateway.entity.GatewayUpstream;
import com.example.demo.gateway.filter.UpstreamReactorLoadBalancer;
import com.example.demo.gateway.repository.GatewayUpstreamRepository;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Configuration
public class UpstreamReactorLoadBalancerConfig {
    @Bean
    public UpstreamReactorLoadBalancer upstreamReactorLoadBalancer(GatewayUpstreamRepository repository){
        Map<String, List<GatewayUpstream>> map = repository.findAll()
                .toStream()
                .collect(Collectors.groupingBy(GatewayUpstream::getServiceId));
        return new UpstreamReactorLoadBalancer(map);

    }


}



